package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.*;
import Entity.*;

/**
 * Servlet implementation class TeachersInformationPageServlet
 */
//@WebServlet("/TeachersInformationPageServlet")
public class TeachersInformationPageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TeachersInformationPageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//解决乱码
		response.setContentType("text/html;charset=utf-8");//解决乱码
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter(); //把编码设置放到输出流获取之前
		String opttype = request.getParameter("opttype");
		//String status = request.getParameter("status");
		//String manageStudent = request.getParameter("manageStudent");
		String teacherNo = "", teacherName, sex, dept, teacherId, pwd, tel;
			

			switch (opttype) {
			case "queryAll":
				int currPage = 1; // 当前页码
				if (request.getParameter("page") != null) { // 判断传递页面是否有效
					currPage = Integer.parseInt(request.getParameter("page")); // 对当前页码赋值
				}
				TeachersInformationDao dao = new TeachersInformationDao();
				List<Teacher> tlist = dao.showAllTeachersByPage(currPage);
				//List<Teacher> tlist=dao.s
				request.setAttribute("tlist", tlist);
				int pages;
				int count = dao.queryRecordCount();
				if (count % Teacher.PAGE_SIZE == 0) {
					pages = count / Teacher.PAGE_SIZE;
				} else {
					pages = count / Teacher.PAGE_SIZE + 1;
				}
				StringBuffer sb = new StringBuffer();
				for (int i = 1; i <= pages; i++) {
					if (i == currPage) {
						sb.append("[" + i + "]");
					} else {
						sb.append("<a href='/UniversityResourceSystem/TeachersInformationPageServlet?page=" + i + "&&status=teacher&&opttype=queryAll'>" + i + "</a>"); // 构建分页条
					}
					sb.append(" ");
				}
				request.setAttribute("bar", sb.toString());
				request.getRequestDispatcher("JSP_administrator/showAllTeachers.jsp").forward(request, response);
				//request.getSession().setAttribute("slist",new StudentsInformationDao().s);
				//response.sendRedirect("showAllStudents.jsp");
				break;
				
			case "add":
				teacherNo = request.getParameter("teacherNo");
				teacherName = request.getParameter("teacherName");
				sex = request.getParameter("sex");
				dept = request.getParameter("dept");
				teacherId = request.getParameter("teacherId");
				pwd = request.getParameter("pwd");
				tel = request.getParameter("tel");
				// Student stu=new
				// Student(studentNo,studentName,sex,dept,major,studentClass,studentId,pwd,tel);
				if (new TeachersInformationDao().addTeacher(teacherNo, teacherName, sex, dept, teacherId, pwd, tel)>0) {
					//response.sendRedirect("StudentsInformationPageServlet?opttype=queryAll");
					out.print("<script type='text/javascript'>alert('添加成功')</script>");
					response.sendRedirect("/UniversityResourceSystem/TeachersInformationPageServlet?opttype=queryAll");
					//response.sendRedirect("showAllStudents.jsp"); 					
				}
				break;
				
			case "delete":
				teacherNo=request.getParameter("teacherNo");
			if(new TeachersInformationDao().deleteTeacherByNo(teacherNo)>0) {
				//out.print("<script type='text/javascript'>alert('删除成功')</script>");
				//response.sendRedirect("showAllStudents.jsp");
				response.sendRedirect("/UniversityResourceSystem/TeachersInformationPageServlet?opttype=queryAll");
			}
			break;
			
			case "alter":
				teacherNo=request.getParameter("teacherNo");
				Teacher teacher=new TeachersInformationDao().queryTeacherByNo(teacherNo);
				request.getSession().setAttribute("teacher", teacher);
				response.sendRedirect("JSP_administrator/alter_Teacher.jsp");
				break;
				
			case "update":
				teacherNo = request.getParameter("teacherNo");
				teacherName = request.getParameter("teacherName");
				sex = request.getParameter("sex");
				dept = request.getParameter("dept");
				teacherId = request.getParameter("teacherId");
				pwd = request.getParameter("pwd");
				tel = request.getParameter("tel");
				Teacher teacher1=new Teacher(teacherNo,teacherName,sex,dept,teacherId,pwd,tel);
				 if(new TeachersInformationDao().alterTeacherInformation(teacher1)>0) {
					 //out.print("<script type='text/javascript'>alert('修改成功')</script>");
					 //response.sendRedirect("showAllStudents.jsp");
					 response.sendRedirect("/UniversityResourceSystem/TeachersInformationPageServlet?opttype=queryAll");
				 }
				 break;
			}
	}

}
